//
//  GouWuCheDetailViewController.m
//  Experss
//
//  Created by wmwMAC on 17/8/3.
//  Copyright © 2017年 嗯，大葱. All rights reserved.
//

#import "GouWuCheDetailViewController.h"
#import "NoBuyDetailTwoCell.h"
#import "NoBuyDetailThirdCell.h"
#import "NoBuyDetailFourCell.h"
#import "NoBuyDetailFiveCell.h"

#import "CustomGuiGeView.h"//规格视图
#import "CustomWuLiuView.h"//商品参数视图
#import "custonWuLiuTwoView.h"//物流视图
#import "InStoreController.h"//进入店铺


@interface GouWuCheDetailViewController ()<UITableViewDelegate, UITableViewDataSource,SDCycleScrollViewDelegate,customGuiGetDelegate,customShangPinCanShuDelegate,customWuLiuTwoDelegate>

{
    SDCycleScrollView *cycleScrollView;
    
    dispatch_source_t _timer;
    
    NSString *tempBtnTitle;//选择的规格参数
}
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSArray *titA;//商品参数标题

//遮盖视图
@property (nonatomic, strong) UIView *maskView;
@property (nonatomic, strong)CustomGuiGeView *customGuiGeView;//规格
@property (nonatomic, strong)CustomWuLiuView *customWuLiuView;//物流
@property (nonatomic, strong)custonWuLiuTwoView *custonViewWuL;//物流

@end

@implementation GouWuCheDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"商品详情";
    
    [self configureNaBar];
    [self configureViewC];
    _titA  = @[@"请选择规格",@"物流说明",@"商品参数"];
    tempBtnTitle  = @"请选择规格";
    
}
- (void)configureNaBar {
    
    //    self.navigationController.navigationBar.translucent = NO;
    //设置导航栏按钮的边距
    UIButton * rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame= CGRectMake(0, 0, 60, 30);
    [rightBtn setImage:[UIImage imageNamed:@"all_fanhui2"] forState:(UIControlStateNormal)];
    [rightBtn addTarget:self action:@selector(handleBackAxtion) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *btn_left = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    UIBarButtonItem *negativeSpacerleft = [[UIBarButtonItem alloc]   initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace   target:nil action:nil];
    /**
     width为负数时，相当于btn向右移动width数值个像素，由于按钮本身和  边界间距为5pix，所以width设为-5时，间距正好调整为0；width为正数 时，正好相反，相当于往左移动width数值个像素
     */
    negativeSpacerleft.width = -20;
    self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacerleft, btn_left, nil];
    
    
    
    UIButton *informationCardBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [informationCardBtn addTarget:self action:@selector(enterehzFilesVC:) forControlEvents:UIControlEventTouchUpInside];
    [informationCardBtn setImage:[UIImage imageNamed:@"fenlei_xiaoxi"] forState:UIControlStateNormal];
    informationCardBtn.frame = CGRectMake(0, 0, 32, 32);
    //[informationCardBtn sizeToFit];
    UIBarButtonItem *informationCardItem = [[UIBarButtonItem alloc] initWithCustomView:informationCardBtn];
    
    
    UIBarButtonItem *fixedSpaceBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
    fixedSpaceBarButtonItem.width = 15;
    
    
    UIButton *settingBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingBtn addTarget:self action:@selector(enterTeamCard:) forControlEvents:UIControlEventTouchUpInside];
    [settingBtn setImage:[UIImage imageNamed:@"shocuang"] forState:UIControlStateNormal];
    //[settingBtn sizeToFit];
    settingBtn.frame = CGRectMake(0, -5, 30, 30);
    UIBarButtonItem *settingBtnItem = [[UIBarButtonItem alloc] initWithCustomView:settingBtn];
    
    self.navigationItem.rightBarButtonItems  = @[informationCardItem,fixedSpaceBarButtonItem,settingBtnItem];
    
}
- (void)handleBackAxtion {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 配置视图控制器
//配置视图控制器
- (void)configureViewC {
    _tableView  = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height) style:(UITableViewStyleGrouped)];
    _tableView.separatorStyle = kSpNone;
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self.view addSubview:_tableView];
    [_tableView registerNib:[UINib nibWithNibName:@"NoBuyDetailTwoCell" bundle:nil] forCellReuseIdentifier:@"NoBuyDetailTwoCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"NoBuyDetailThirdCell" bundle:nil] forCellReuseIdentifier:@"NoBuyDetailThirdCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"NoBuyDetailFourCell" bundle:nil] forCellReuseIdentifier:@"NoBuyDetailFourCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"NoBuyDetailFiveCell" bundle:nil] forCellReuseIdentifier:@"NoBuyDetailFiveCell"];
    
    
    
    UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 45)];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width , 10)];
    lineView.backgroundColor = KShouYeBg;
    [footerView addSubview:lineView];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake((kScreen_Width - 180) * 0.5, 20, 180, 17)];
    label.font = [UIFont systemFontOfSize:12 * kScreen_Width / 375];
    label.text = @"继续拖动，查看图文详情";
    label.textAlignment = NSTextAlignmentCenter;
    [footerView addSubview:label];
    UIImageView *img = [[UIImageView alloc] init];
    img.contentMode = UIViewContentModeScaleAspectFit;
    img.frame = CGRectMake(CGRectGetMinX(label.frame), CGRectGetMinY(label.frame),15, 15);
    img.image = [UIImage imageNamed:@"上箭头"];
    [footerView addSubview:img];
    
    self.tableView.tableFooterView = footerView;
    
    
    
    
    UIButton *buyCarBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    buyCarBtn.frame = CGRectMake(0, kScreen_Height - 50, kScreen_Width/2, 50);
    [buyCarBtn setTitle:@"加入购物车" forState:(UIControlStateNormal)];
    [buyCarBtn setBackgroundImage:[UIImage imageNamed:@"chengkuai"] forState:(UIControlStateNormal)];
    [self.view addSubview:buyCarBtn];
    
    UIButton *buyBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    buyBtn.frame = CGRectMake(CGRectGetMaxX(buyCarBtn.frame), kScreen_Height - 50, kScreen_Width/2, 50);
    [buyBtn setTitle:@"立即购买" forState:(UIControlStateNormal)];
    [buyBtn setBackgroundImage:[UIImage imageNamed:@"bg1"] forState:(UIControlStateNormal)];
    [self.view addSubview:buyBtn];
    
    
    _customGuiGeView = [[CustomGuiGeView alloc] initWithFrame:CGRectMake(0, kScreen_Height, kScreenWidth, kScreen_Height)];
    _customGuiGeView.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:_customGuiGeView];
    
    _customWuLiuView = [[CustomWuLiuView alloc] initWithFrame:CGRectMake(0, kScreen_Height, kScreenWidth, kScreen_Height)];
    _customWuLiuView.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:_customWuLiuView];
    
    _custonViewWuL = [[custonWuLiuTwoView alloc] initWithFrame:CGRectMake(0, kScreen_Height, kScreenWidth, kScreen_Height)];
    _custonViewWuL.delegate = self;
    [[UIApplication sharedApplication].keyWindow addSubview:_custonViewWuL];
    
    
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 1) {
        return 3;
    }else {
        return 1;
    }
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if(indexPath.section == 0) {
        return 175;
    }
    else if(indexPath.section == 1){
        return 60;
    }else if (indexPath.section == 2){
        return 160;
    }else {
        return 101;
    }
    
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if(indexPath.section == 0){
        NoBuyDetailTwoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoBuyDetailTwoCell"];
        cell.selectionStyle = kSeNone;
        return cell;
    }else if (indexPath.section == 1){
        
        NoBuyDetailThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoBuyDetailThirdCell"];
        
        cell.selectionStyle = kSeNone;
        if (indexPath.row == 0) {
            cell.titLB.text = tempBtnTitle;//更新cell的内容
        }else {
            cell.titLB.text = _titA[indexPath.row];
        }
        
        return cell;
    }else if(indexPath.section == 2){
        NoBuyDetailFourCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoBuyDetailFourCell"];
        cell.selectionStyle = kSeNone;
        return cell;
    }else {
        NoBuyDetailFiveCell *cell = [tableView dequeueReusableCellWithIdentifier:@"NoBuyDetailFiveCell"];
        cell.selectionStyle = kSeNone;
        
        [cell.goInSuperMark addTarget:self action:@selector(handleGoInSuperMark:) forControlEvents:(UIControlEventTouchUpInside)];
        
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 1) {
        if (indexPath.row == 0) {
            //弹出规格视图
            [self popOneView];
        }
        if (indexPath.row == 1) {
            //弹出物流说明视图
            [self popThirdView];
        }
        if (indexPath.row == 2) {
            //弹出商品参数
            [self popTwoView];
        }
        
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0){
        return 200;
    }else if ( section == 1) {
        return 0;
    }else {
        return 10;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *bgV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 10)];
    bgV.backgroundColor = KShouYeBg;
    
    if (section == 0) {
        
        if (cycleScrollView) {
            [cycleScrollView removeFromSuperview];
        }
        // 创建不带标题的图片轮播器
        cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, kScreen_Width, 201) imageURLStringsGroup:nil];//最后设置占位
        
        //网络数据
        //cycleScrollView.imageURLStringsGroup = [NSArray arrayWithArray:_imageVarray];
        
        //本地图片
        cycleScrollView.localizationImageNamesGroup = @[@"xianshimiaosha_banner",@"满20减3"];
        cycleScrollView.backgroundColor =[ UIColor clearColor];
        
        cycleScrollView.showPageControl = NO;
        cycleScrollView.autoScroll = NO;//不自动滚动
        cycleScrollView.delegate = self;
        [bgV addSubview:cycleScrollView];
        
        
    }
    
    
    return bgV;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *footV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 10)];
    footV.backgroundColor = KShouYeBg;
    return footV;
}

- (void)popOneView {
    [UIView animateWithDuration:0.2 animations:^{
        _customGuiGeView.frame = CGRectMake(0, 0, kScreenWidth, kScreen_Height);
    }];
    
}
- (void)popTwoView {
    [UIView animateWithDuration:0.2 animations:^{
        _customWuLiuView.frame = CGRectMake(0, 0, kScreenWidth, kScreen_Height);
    }];
}
- (void)popThirdView {
    [UIView animateWithDuration:0.2 animations:^{
        _custonViewWuL.frame = CGRectMake(0, 0, kScreenWidth, kScreen_Height);
    }];
}
#pragma mark 弹出规格视图的代理事件
//第一个弹出视图
- (void)clooseBtnAction :(UIButton *)sender{
    [UIView animateWithDuration:0.2 animations:^{
        _customGuiGeView.frame = CGRectMake(0, kScreen_Height, kScreenWidth, kScreen_Height);
    }];
}
//确定购买
- (void)sureBuyBtnAction:(UIButton *)sender :(NSString *)btnTitle {
    [UIView animateWithDuration:0.2 animations:^{
        _customGuiGeView.frame = CGRectMake(0, kScreen_Height, kScreenWidth, kScreen_Height);
    }];
    tempBtnTitle = btnTitle;//把点击产品参数的值赋值给中间参数，然后刷新
    //一个cell刷新
    NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:2];
    [_tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];
    
    
}




//第二个弹出视图
- (void)rePopShangpinXiangQingView:(UIButton *)sender {
    
    _customWuLiuView.frame = CGRectMake(0, kScreen_Height, kScreenWidth, kScreen_Height);
}
//确定按钮
- (void)sureShangPinCanshuView:(UIButton *)sender {
    _customWuLiuView.frame = CGRectMake(0, kScreen_Height, kScreenWidth, kScreen_Height);
    
}


//第三个视图

-(void)rePopWuLiuView:(UIButton *)sender {
    _custonViewWuL.frame = CGRectMake(0, kScreen_Height, kScreenWidth, kScreen_Height);
    
}
- (void)sureWuLiuView:(UIButton *)sender {
    _custonViewWuL.frame = CGRectMake(0, kScreen_Height, kScreenWidth, kScreen_Height);
}







#pragma  mark 轮播图的点击代理事件
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index {
    NSLog(@"----%ld",(long)index);
    
    
}
#pragma mark 导航栏按钮的点击事件

-(void)enterehzFilesVC :(UIBarButtonItem *)message {
    [self.view showTextNoActivity:@"我是消息按钮" timeLength:1];
    
    
}

- (void)enterTeamCard:(UIBarButtonItem *)coll {
    
    [self.view showTextNoActivity:@"我是收藏按钮" timeLength:1];
    
}


#pragma  mark 倒计时计算代码
-(NSString *)getyyyymmdd{
    NSDate *now = [NSDate date];
    NSDateFormatter *formatDay = [[NSDateFormatter alloc] init];
    formatDay.dateFormat = @"yyyy-MM-dd";
    NSString *dayStr = [formatDay stringFromDate:now];
    
    return dayStr;
    
}


#pragma mark 进入商店的按钮的点击事件
//进店
- (void)handleGoInSuperMark:(UIButton *)sender {
    InStoreController *store = [[InStoreController alloc] init];
    [self.navigationController pushViewController:store animated:YES];
}

@end
